
#include "stdafx.h"
#include "ConstDef.h"
#include "ReloadCode.h"
#include "ReloadApp.h"
#include "ReloadPacket.h"
#include "ReloadOverlay.h"
#include "ReloadConfigure.h"

namespace reload
{
	StoreKindData::StoreKindData()
	{

	}

	StoreKindData::~StoreKindData()
	{

	}

	/////////////////////////////////////////////////////////////////////////////////////

	StoreReq::StoreReq()
		:replicaNumber_(0)
	{

	}

	StoreReq::~StoreReq()
	{
		replicaNumber_ = 0;
	}

	retT StoreReq::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(resource_.readFromBuffer(_pBuffer));
		R(_pBuffer->ntohRead(replicaNumber_));
		R(kindData_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT StoreReq::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(resource_.writeToBuffer(_pBuffer));
		R(_pBuffer->htonWrite(replicaNumber_));
		R(kindData_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t StoreReq::size()
	{
		return resource_.size() + sizeof(replicaNumber_) + kindData_.size();
	}

	retT StoreReq::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(resource_.attachRootElement(_pRootElement));
		R(kindData_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	StoreKindResponse::StoreKindResponse()
		:kind_(0),generationCounter_(0)
	{

	}

	StoreKindResponse::~StoreKindResponse()
	{
		kind_ = 0;
		generationCounter_ = 0;
	}

	retT StoreKindResponse::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(kind_));
		R(_pBuffer->ntohRead(generationCounter_));
		R(replicas_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT StoreKindResponse::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(kind_));
		R(_pBuffer->htonWrite(generationCounter_));
		R(replicas_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t StoreKindResponse::size()
	{
		return sizeof(kind_) + sizeof(generationCounter_) + replicas_.size();
	}

	retT StoreKindResponse::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(replicas_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	///////////////////////////////////////////////////////////////////////////////////

	UnknownKindsInErrorInfo::UnknownKindsInErrorInfo()
	{

	}

	UnknownKindsInErrorInfo::~UnknownKindsInErrorInfo()
	{

	}

	retT UnknownKindsInErrorInfo::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(unknownKinds_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT UnknownKindsInErrorInfo::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(unknownKinds_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t UnknownKindsInErrorInfo::size()
	{
		return unknownKinds_.size();
	}

	retT UnknownKindsInErrorInfo::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	StoreAns::StoreAns()
	{

	}

	StoreAns::~StoreAns()
	{

	}

	retT StoreAns::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(kindResponses_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT StoreAns::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(kindResponses_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t StoreAns::size()
	{
		return kindResponses_.size();
	}

	retT StoreAns::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(kindResponses_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	///////////////////////////////////////////////////////////////////////////////////

	ArrayRange::ArrayRange()
		:first_(0),last_(0)
	{

	}

	ArrayRange::~ArrayRange()
	{
		first_ = 0;
		last_ = 0;
	}

	retT ArrayRange::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(first_));
		R(_pBuffer->ntohRead(last_));
		READFROMBUFFER_END;
	}

	retT ArrayRange::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(first_));
		R(_pBuffer->htonWrite(last_));
		WRITETOBUFFER_END;
	}

	std::size_t ArrayRange::size()
	{
		return sizeof(first_) + sizeof(last_);
	}

	bool operator<( const ArrayRange& _arrayRangeLeft, const ArrayRange& _arrayRangeRight )
	{
		return _arrayRangeLeft.first_ < _arrayRangeRight.first_;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	StoredDataSpecifier::StoredDataSpecifier()
		:kind_(0),generation_(0),length_(0),pKindBlock_(NULL)
	{

	}

	StoredDataSpecifier::~StoredDataSpecifier()
	{
		kind_ = 0;
		generation_ = 0;
		length_ = 0;
		pKindBlock_ = NULL;
	}

	int StoredDataSpecifier::init( asyframe::uint32 _overlayNameHash, KindId _kind )
	{
		boost::shared_ptr<ReloadApp> pReloadApp = boost::dynamic_pointer_cast<ReloadApp>(asyframe::getApp());
		if(!pReloadApp)
			RET(ERROR_COMMON_SHARED_PTR_NULL);
		boost::unordered_map<KindId, KindBlock>& requiredKinds = pReloadApp->getReloadConfigure()->overlay_[_overlayNameHash].requiredKinds_;
		boost::unordered_map<KindId, KindBlock>::iterator it = requiredKinds.find(_kind);
		if(it != requiredKinds.end())
		{
			pKindBlock_ = &it->second;
			kind_ = _kind;
		}
		else
			pKindBlock_ = NULL;
		RETSUCCESS;
	}

	int StoredDataSpecifier::init( std::string _overlayInstanceName, KindId _kind )
	{
		boost::shared_ptr<ReloadApp> pReloadApp = boost::dynamic_pointer_cast<ReloadApp>(asyframe::getApp());
		if(!pReloadApp)
			RET(ERROR_COMMON_SHARED_PTR_NULL);
		if(boost::shared_ptr<ReloadOverlay> pReloadOverlay = pReloadApp->getOverlay(_overlayInstanceName))
		{
			R(init(pReloadOverlay->overlayNameHash_, _kind));
		}
		else
			RETNL(ERROR_COMMON_SHARED_PTR_NULL);
		RETSUCCESS;
	}

	retT StoredDataSpecifier::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(kind_));
		if(boost::shared_ptr<ReloadPacket> pReloadPacket = boost::dynamic_pointer_cast<ReloadPacket>(pRootElement_.lock()))
		{
			boost::unordered_map<KindId, KindBlock>& requiredKinds = boost::dynamic_pointer_cast<ReloadApp>(asyframe::getApp())->getReloadConfigure()->overlay_[pReloadPacket->forwardingHeader_.overlay_].requiredKinds_;
			boost::unordered_map<KindId, KindBlock>::iterator it = requiredKinds.find(kind_);
			if(it != requiredKinds.end())
			{
				pKindBlock_ = &it->second;
			}
			else
				pKindBlock_ = NULL;
		}
		else
			RET(ERROR_COMMON_SHARED_PTR_NULL);
		R(_pBuffer->ntohRead(generation_));
		R(_pBuffer->ntohRead(length_));
		if(pKindBlock_ != NULL)
		{
			switch(pKindBlock_->kind_.dataModel_)
			{
			case DataModel_Single:break;
			case DataModel_Array:R(modelSpecifier_.indices_.readFromBuffer(_pBuffer));break;
			case DataModel_Dictionary:R(modelSpecifier_.keys_.readFromBuffer(_pBuffer));break;
			default:R(_pBuffer->consume(length_));RET(INFO_PROTOCOL_RELOAD_UNKNOWN_DATAMODEL_TYPE);
			}
		}
		else
			R(_pBuffer->consume(length_));
		READFROMBUFFER_END;
	}

	retT StoredDataSpecifier::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		if(boost::shared_ptr<ReloadPacket> pReloadPacket = boost::dynamic_pointer_cast<ReloadPacket>(pRootElement_.lock()))
		{
			boost::unordered_map<KindId, KindBlock>& requiredKinds = boost::dynamic_pointer_cast<ReloadApp>(asyframe::getApp())->getReloadConfigure()->overlay_[pReloadPacket->forwardingHeader_.overlay_].requiredKinds_;
			boost::unordered_map<KindId, KindBlock>::iterator it = requiredKinds.find(kind_);
			if(it != requiredKinds.end())
			{
				pKindBlock_ = &it->second;
			}
			else
				RET(INFO_PROTOCOL_RELOAD_UNKNOWN_KIND);
		}
		else
			RET(ERROR_COMMON_SHARED_PTR_NULL);
		R(_pBuffer->htonWrite(kind_));
		R(_pBuffer->htonWrite(generation_));
		length_ = size() - sizeof(kind_) - sizeof(generation_) - sizeof(length_);
		R(_pBuffer->htonWrite(length_));
		switch(pKindBlock_->kind_.dataModel_)
		{
		case DataModel_Single:break;
		case DataModel_Array:R(modelSpecifier_.indices_.writeToBuffer(_pBuffer));break;
		case DataModel_Dictionary:R(modelSpecifier_.keys_.writeToBuffer(_pBuffer));break;
		default:R(_pBuffer->produce(length_));RET(INFO_PROTOCOL_RELOAD_UNKNOWN_DATAMODEL_TYPE);
		}
		WRITETOBUFFER_END;
	}

	std::size_t StoredDataSpecifier::size()
	{
		if(boost::shared_ptr<ReloadPacket> pReloadPacket = boost::dynamic_pointer_cast<ReloadPacket>(pRootElement_.lock()))
		{
			boost::unordered_map<KindId, KindBlock>& requiredKinds = boost::dynamic_pointer_cast<ReloadApp>(asyframe::getApp())->getReloadConfigure()->overlay_[pReloadPacket->forwardingHeader_.overlay_].requiredKinds_;
			boost::unordered_map<KindId, KindBlock>::iterator it = requiredKinds.find(kind_);
			if(it != requiredKinds.end())
			{
				pKindBlock_ = &it->second;
			}
			else
				pKindBlock_ = NULL;
		}
		else
			RU(ERROR_COMMON_SHARED_PTR_NULL, 0);
		std::size_t totalSize = 0;
		totalSize += sizeof(kind_);
		totalSize += sizeof(generation_);
		totalSize += sizeof(length_);
		if(pKindBlock_ != NULL)
		{
			switch(pKindBlock_->kind_.dataModel_)
			{
			case DataModel_Single:break;
			case DataModel_Array:totalSize += modelSpecifier_.indices_.size();break;
			case DataModel_Dictionary:totalSize += modelSpecifier_.keys_.size();break;
			default:totalSize += length_;break;
			}
		}
		else
			totalSize += length_;
		return totalSize;
	}

	retT StoredDataSpecifier::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(modelSpecifier_.indices_.attachRootElement(_pRootElement));
		R(modelSpecifier_.keys_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	FetchReq::FetchReq()
	{

	}

	FetchReq::~FetchReq()
	{

	}

	retT FetchReq::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(resource_.readFromBuffer(_pBuffer));
		R(specifiers_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT FetchReq::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(resource_.writeToBuffer(_pBuffer));
		R(specifiers_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t FetchReq::size()
	{
		return resource_.size() + specifiers_.size();
	}

	retT FetchReq::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(resource_.attachRootElement(_pRootElement));
		R(specifiers_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	FetchKindResponse::FetchKindResponse()
		//:kind_(0),generation_(0)
	{

	}

	FetchKindResponse::~FetchKindResponse()
	{
		//kind_ = 0;
		//generation_ = 0;
	}

	//retT FetchKindResponse::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	//{
	//	READFROMBUFFER_START;
	//	R(_pBuffer->ntohRead(kind_));
	//	DataModelT dataModel;
	//	if(boost::shared_ptr<ReloadPacket> pReloadPacket = boost::dynamic_pointer_cast<ReloadPacket>(pRootElement_.lock()))
	//	{
	//		boost::unordered_map<KindId, KindBlock>& requiredKinds = boost::dynamic_pointer_cast<ReloadApp>(asyframe::getApp())->getReloadConfigure()->overlay_[pReloadPacket->forwardingHeader_.overlay_].requiredKinds_;
	//		boost::unordered_map<KindId, KindBlock>::iterator it = requiredKinds.find(kind_);
	//		if(it == requiredKinds.end())
	//			RET(INFO_PROTOCOL_RELOAD_UNKNOWN_KIND);
	//	}
	//	else
	//		RET(ERROR_COMMON_SHARED_PTR_NULL);
	//	R(_pBuffer->ntohRead(generation_));
	//	R(values_.readFromBuffer(_pBuffer, (boost::function1< retT, StoredData * >)boost::bind(&StoredData::init, &it->second, _1)));
	//	READFROMBUFFER_END;
	//}
	//
	//retT FetchKindResponse::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	//{
	//	WRITETOBUFFER_START;
	//	R(_pBuffer->htonWrite(kind_));
	//	R(_pBuffer->htonWrite(generation_));
	//	R(values_.writeToBuffer(_pBuffer));
	//	WRITETOBUFFER_END;
	//}
	//
	//std::size_t FetchKindResponse::size()
	//{
	//	return sizeof(kind_) + sizeof(generation_) + values_.size();
	//}
	//
	//retT FetchKindResponse::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	//{
	//	R(asyframe::PacketElement::attachRootElement(_pRootElement));
	//	R(values_.attachRootElement(_pRootElement));
	//	RETSUCCESS;
	//}

	/////////////////////////////////////////////////////////////////////////////////////

	FetchAns::FetchAns()
	{

	}

	FetchAns::~FetchAns()
	{

	}

	retT FetchAns::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(kindResponses_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT FetchAns::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(kindResponses_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t FetchAns::size()
	{
		return kindResponses_.size();
	}

	retT FetchAns::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(kindResponses_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	StatReq::StatReq()
	{

	}

	StatReq::~StatReq()
	{

	}

	retT StatReq::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(resource_.readFromBuffer(_pBuffer));
		R(specifiers_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT StatReq::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(resource_.writeToBuffer(_pBuffer));
		R(specifiers_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t StatReq::size()
	{
		return resource_.size() + specifiers_.size();
	}

	retT StatReq::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(resource_.attachRootElement(_pRootElement));
		R(specifiers_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	MetaData::MetaData()
		:exists_(Boolean_False),valueLength_(0),hashAlgorithm_(HashAlgorithm_None)
	{

	}

	MetaData::~MetaData()
	{
		exists_ = Boolean_False;
		valueLength_ = 0;
		hashAlgorithm_ = HashAlgorithm_None;
	}

	retT MetaData::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(exists_));
		R(_pBuffer->ntohRead(valueLength_));
		R(_pBuffer->ntohRead(hashAlgorithm_));
		R(hashvalue_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT MetaData::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(exists_));
		R(_pBuffer->htonWrite(valueLength_));
		R(_pBuffer->htonWrite(hashAlgorithm_));
		R(hashvalue_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t MetaData::size()
	{
		return sizeof(exists_) + sizeof(valueLength_) + sizeof(hashAlgorithm_) + hashvalue_.size();
	}

	retT MetaData::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(hashvalue_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	ArrayEntryMeta::ArrayEntryMeta()
		:index_(0)
	{

	}

	ArrayEntryMeta::~ArrayEntryMeta()
	{
		index_ = 0;
	}

	retT ArrayEntryMeta::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(index_));
		R(value_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT ArrayEntryMeta::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(index_));
		R(value_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t ArrayEntryMeta::size()
	{
		return sizeof(index_) + value_.size();
	}

	retT ArrayEntryMeta::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(value_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	DictionaryEntryMeta::DictionaryEntryMeta()
	{

	}

	DictionaryEntryMeta::~DictionaryEntryMeta()
	{

	}

	retT DictionaryEntryMeta::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(key_.readFromBuffer(_pBuffer));
		R(value_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT DictionaryEntryMeta::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(key_.writeToBuffer(_pBuffer));
		R(value_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t DictionaryEntryMeta::size()
	{
		return key_.size() + value_.size();
	}

	retT DictionaryEntryMeta::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(key_.attachRootElement(_pRootElement));
		R(value_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	MetaDataValue::MetaDataValue()
	{

	}

	MetaDataValue::~MetaDataValue()
	{

	}

	retT MetaDataValue::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		switch(model_)
		{
		case DataModel_Single:R(singleValueEntry_.readFromBuffer(_pBuffer));break;
		case DataModel_Array:R(arrayEntry_.readFromBuffer(_pBuffer));break;
		case DataModel_Dictionary:R(dictionaryEntry_.readFromBuffer(_pBuffer));break;
		default:break;
		}
		READFROMBUFFER_END;
	}

	retT MetaDataValue::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		switch(model_)
		{
		case DataModel_Single:R(singleValueEntry_.writeToBuffer(_pBuffer));break;
		case DataModel_Array:R(arrayEntry_.writeToBuffer(_pBuffer));break;
		case DataModel_Dictionary:R(dictionaryEntry_.writeToBuffer(_pBuffer));break;
		default:break;
		}
		WRITETOBUFFER_END;
	}

	std::size_t MetaDataValue::size()
	{
		std::size_t totalSize = 0;
		switch(model_)
		{
		case DataModel_Single:totalSize += singleValueEntry_.size();break;
		case DataModel_Array:totalSize += arrayEntry_.size();break;
		case DataModel_Dictionary:totalSize += dictionaryEntry_.size();break;
		default:break;
		}
		return totalSize;
	}

	retT MetaDataValue::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(singleValueEntry_.attachRootElement(_pRootElement));
		R(arrayEntry_.attachRootElement(_pRootElement));
		R(dictionaryEntry_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	StoredMetaData::StoredMetaData()
		:valueLength_(0),storageTime_(0),lifetime_(0)
	{

	}

	StoredMetaData::~StoredMetaData()
	{
		valueLength_ = 0;
		storageTime_ = 0;
		lifetime_ = 0;
	}

	retT StoredMetaData::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(valueLength_));
		R(_pBuffer->ntohRead(storageTime_));
		R(_pBuffer->ntohRead(lifetime_));
		R(metadata_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT StoredMetaData::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(valueLength_));
		R(_pBuffer->htonWrite(storageTime_));
		R(_pBuffer->htonWrite(lifetime_));
		R(metadata_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t StoredMetaData::size()
	{
		return sizeof(valueLength_) + sizeof(storageTime_) + sizeof(lifetime_) + metadata_.size();
	}

	retT StoredMetaData::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(metadata_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	retT StoredMetaData::init( DataModelT _dataModel, StoredMetaData* _pStoredData )
	{
		_pStoredData->metadata_.model_ = _dataModel;
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	StatKindResponse::StatKindResponse()
		:kind_(0),generation_(0)
	{

	}

	StatKindResponse::~StatKindResponse()
	{
		kind_ = 0;
		generation_ = 0;
	}

	retT StatKindResponse::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(kind_));
		DataModelT dataModel;
		if(boost::shared_ptr<ReloadPacket> pReloadPacket = boost::dynamic_pointer_cast<ReloadPacket>(pRootElement_.lock()))
		{
			boost::unordered_map<KindId, KindBlock>& requiredKinds = boost::dynamic_pointer_cast<ReloadApp>(asyframe::getApp())->getReloadConfigure()->overlay_[pReloadPacket->forwardingHeader_.overlay_].requiredKinds_;
			boost::unordered_map<KindId, KindBlock>::iterator it = requiredKinds.find(kind_);
			if(it != requiredKinds.end())
			{
				dataModel = it->second.kind_.dataModel_;
			}
			else
				RET(INFO_PROTOCOL_RELOAD_UNKNOWN_KIND);
		}
		else
			RET(ERROR_COMMON_SHARED_PTR_NULL);
		R(_pBuffer->ntohRead(generation_));
		R(values_.readFromBuffer(_pBuffer, (boost::function1< retT, StoredMetaData * >)boost::bind(&StoredMetaData::init, dataModel, _1)));
		READFROMBUFFER_END;
	}

	retT StatKindResponse::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(kind_));
		R(_pBuffer->htonWrite(generation_));
		R(values_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t StatKindResponse::size()
	{
		return sizeof(kind_) + sizeof(generation_) + values_.size();
	}

	retT StatKindResponse::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(values_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	StatAns::StatAns()
	{

	}

	StatAns::~StatAns()
	{

	}

	retT StatAns::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(kindResponses_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT StatAns::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(kindResponses_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t StatAns::size()
	{
		return kindResponses_.size();
	}

	retT StatAns::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(kindResponses_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	FindReq::FindReq()
	{

	}

	FindReq::~FindReq()
	{

	}

	retT FindReq::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(resource_.readFromBuffer(_pBuffer));
		R(kinds_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT FindReq::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(resource_.writeToBuffer(_pBuffer));
		R(kinds_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t FindReq::size()
	{
		return resource_.size() + kinds_.size();
	}

	retT FindReq::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(resource_.attachRootElement(_pRootElement));
		R(kinds_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	FindKindData::FindKindData()
		:kind_(0)
	{

	}

	FindKindData::~FindKindData()
	{
		kind_ = 0;
	}

	retT FindKindData::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(kind_));
		R(closest_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT FindKindData::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(kind_));
		R(closest_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t FindKindData::size()
	{
		return sizeof(kind_) + closest_.size();
	}

	retT FindKindData::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(closest_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	FindAns::FindAns()
	{

	}

	FindAns::~FindAns()
	{

	}

	retT FindAns::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(results_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT FindAns::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(results_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t FindAns::size()
	{
		return results_.size();
	}

	retT FindAns::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(results_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}

	/////////////////////////////////////////////////////////////////////////////////////

	TurnServer::TurnServer()
		:iteration_(0)
	{

	}

	TurnServer::~TurnServer()
	{
		iteration_ = 0;
	}

	retT TurnServer::readFromBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		READFROMBUFFER_START;
		R(_pBuffer->ntohRead(iteration_));
		R(serverAddress_.readFromBuffer(_pBuffer));
		READFROMBUFFER_END;
	}

	retT TurnServer::writeToBuffer( boost::shared_ptr<asyframe::IoBuffer> _pBuffer )
	{
		WRITETOBUFFER_START;
		R(_pBuffer->htonWrite(iteration_));
		R(serverAddress_.writeToBuffer(_pBuffer));
		WRITETOBUFFER_END;
	}

	std::size_t TurnServer::size()
	{
		return sizeof(iteration_) + serverAddress_.size();
	}

	retT TurnServer::attachRootElement( boost::weak_ptr<asyframe::PacketElement> _pRootElement )
	{
		R(asyframe::PacketElement::attachRootElement(_pRootElement));
		R(serverAddress_.attachRootElement(_pRootElement));
		RETSUCCESS;
	}
}
